home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0411.ZIP / ENCRYPT2.C < prev    next >
Text File  |  1985-09-19  |  4KB  |  148 lines

  1. #include "stdio.h"
  2. main(argc,argv)
  3.     int argc;
  4.     char *argv[];
  5.     {
  6.     int  nochars,
  7.      lineno,
  8.      passposn,
  9.      swposn,
  10.      infile,
  11.      outfile,
  12.      passlen,
  13.      buffposn,
  14.      buffsize;
  15.     char password[9],
  16.      passchek[9],
  17.      outbuff[513],
  18.      inbuff[513],
  19.     *outptr,
  20.     *inptr;
  21.     if (argc == 1)
  22.     {
  23.     exit(0);
  24.     }
  25.     if (argv[1][0] == '#')
  26.     {
  27.     puts("\n");
  28.     puts("Program: ENCRYPT\n");
  29.     puts("Author : Peter Townsend\n");
  30.     puts("Date   : 19Sep85\n");
  31.     puts("Time   : 18:02\n");
  32.     puts("Version: 1.2\n");
  33.     exit(0);
  34.     }
  35.     if (argv[1][0] == '?')
  36.     {
  37.     puts("\n");
  38.     puts("This program reads a file and, character by character, adds a\n");
  39.     puts("key word (modulo 256) to the character.\n");
  40.     puts("\n");
  41.     puts("There are 3 parameters on the command line:\n");
  42.     puts("  1. Input filename  - the name of the file which is to be\n");
  43.     puts("                       (de-)encrypted.\n");
  44.     puts("  2. Output filename - the name of the file into which the\n");
  45.     puts("                       (de-)encrypted data is to be placed.\n");
  46.     puts("  3. Encrypt switch  - literal, either /ON or /OFF indicating\n");
  47.     puts("                       whether, respectively, the data is to\n");
  48.     puts("                       be encrypted or de-encrypted.\n");
  49.     puts("\n");
  50.     puts("Syntax: ENCRYPT [input filename] [output filename] /on|/off \n");
  51.     puts("\n");
  52.     puts("Example:  ENCRYPT coded.dbf myfile.dbf /on\n");
  53.     puts("          will ask for a password which it will use to create\n");
  54.     puts("          the coded file coded.dbf from myfile.dbf.\n");
  55.     puts("Example:  ENCRYPT coded.dbf myfile.dbf /off\n");
  56.     puts("          will ask for a password which it will use to create\n");
  57.     puts("          an uncoded file myfile.dbf from coded.dbf.\n");
  58.     puts("\n");
  59.     puts("NB If more than 1 person encrypts a file, each using their\n");
  60.     puts("   own password, then the result is not legible to either with-\n");
  61.     puts("   out the other applying their password.");
  62.     exit(0);
  63.     }
  64.     if (argc == 2)
  65.     {
  66.     printf("\nMissing Output Filename\n");
  67.     exit(1);
  68.     }
  69.     if (argc == 3)
  70.     {
  71.     printf("\nMissing Encrypt switch\n");
  72.     exit(1);
  73.     }
  74.     if (strcmp(argv[1],argv[2]) == 0)
  75.     {
  76.     printf("\nInput filename and output filename cannot be the same.\n");
  77.     exit(1);
  78.     }
  79.     /* Convert the switch to upper case */
  80.     for(swposn=0;swposn<strlen(argv[3]);swposn++)
  81.     {
  82.     argv[3][swposn] = toupper(argv[3][swposn]);
  83.     }
  84.     if (strcmp(argv[3],"/ON") != 0)
  85.     {
  86.     if (strcmp(argv[3],"/OFF") != 0)
  87.         {
  88.         printf("\nInvalid encrypt switch ---> %s\n",argv[3]);
  89.         exit(1);
  90.         }
  91.     }
  92.     if ((infile = open(argv[1],0)) == -1)
  93.     {
  94.     printf("\nCannot open input file ---> %s\n",argv[1]);
  95.     exit(1);
  96.     }
  97.     if ((outfile = open(argv[2],1)) == -1)
  98.     {
  99.     if ((outfile = creat(argv[2])) == -1)
  100.         {
  101.         printf("\nCannot open/create output file ---> %s\n",argv[2]);
  102.         exit(1);
  103.         }
  104.     }
  105.     printf("\nPlease enter your password (max of 8 characters) ===> ");
  106.     scanf("%8s",password);
  107.     for(lineno=0;lineno<24;lineno++)
  108.     {
  109.     puts("\n");
  110.     }
  111.     printf("\nPlease re-enter your password (max of 8 characters) ===> ");
  112.     scanf("%8s",passchek);
  113.     for(lineno=0;lineno<24;lineno++)
  114.     {
  115.     puts("\n");
  116.     }
  117.     if (strcmp(passchek,password) != 0)
  118.     {
  119.     printf("\nPasswords do not match ... ENCRYPT terminating.\n");
  120.     exit(1);
  121.     }
  122.     passlen = strlen(password);
  123.     if (strcmp(argv[3],"/OFF") == 0)
  124.     {
  125.     for(passposn=0;passposn<passlen;passposn++)
  126.         {
  127.         password[passposn] = 256 - password[passposn];
  128.         }
  129.     }
  130.     passposn = passlen;
  131.     printf("\nEncrypting ... Do NOT disturb.\n");
  132.     buffsize = 512;
  133.     inptr = *inbuff;
  134.     outptr = *outbuff;
  135.     while ((nochars=fread(inptr,sizeof(*inptr),buffsize,infile)) != EOF)
  136.     {
  137.     for (buffposn=0;buffposn<nochars;buffposn++)
  138.         {
  139.         passposn = (++passposn) % passlen;
  140.         outbuff[buffposn] = (inbuff[buffposn] + password[passposn]) % 256;
  141.         }
  142.     fwrite(outptr,sizeof(*outptr),nochars,outfile);
  143.     }
  144.     printf("\nEncryption finished.\n");
  145.     fclose(infile);
  146.     fclose(outfile);
  147.     }
  148.